using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "NewMeleeWeapon", menuName = "Weapons/Melee Weapon", order = 2)]
public class MeleeWeapon : Weapon
{
    public float attackRange = 1f;
    public float attackAngle = 60f;
    public LayerMask enemyLayer;

    protected override void PerformAttack()
    {
        Debug.Log("Performing melee attack");
        // Play attack animation
        EventManager.TriggerEvent(new MeleeAttackStartedEvent(this));

        // Detect enemies in range
        Collider[] hitEnemies = Physics.OverlapSphere(
            firePoint.position,
            attackRange,
            enemyLayer
        );


        foreach (var enemy in hitEnemies)
        {
            // Check angle
            Vector3 directionToEnemy = (enemy.transform.position - firePoint.position).normalized;
            float angle = Vector3.Angle(firePoint.forward, directionToEnemy);



            if (angle <= attackAngle / 2)
            {
                EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
                if (enemyHealth != null)
                {
                    enemyHealth.TakeDamage(damage);
                }
            }
        }
    }

    
}
